import requests
import json
import get_md5
import readConfig
import get_headers


class get_Token(object):
    def get_token(self):  # 获取激活接口返回的token，保存为self.token
        timestamp, md5 = get_md5.get_Md5().get_md5()
        sn = readConfig.readConfig("data", "sn")
        url = readConfig.readConfig("open", "get_token")  # 根据平台修改food为餐饮，open为金服，ratail是零售
        headers = get_headers.get_headers()
        # headers = readExcel.readExcel().readExcel()
        # print(headers)
        # url1 = 'http://open.dairphone.cn/device/status'
        # url = 'https://retail-device.triumen.cn/device/check_status'
        data = {
            "sn": sn,
            "check_code": md5,
            "time": timestamp,
            "version": "3.5.6"
        }
        result1 = requests.post(url, data=data, headers=headers)
        results1 = result1.json()
        # print(json.dumps(results1, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        try:
            token = results1['data']['token']
            uid = results1['data']['uid']
        except Exception:
            print(Exception)
            print(json.dumps(results1, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        else:
            return token, uid

# get_Token().get_token()
